package com.cgs.query.monitor

import io.micrometer.core.instrument.Metrics
import io.micrometer.core.instrument.Timer
import io.micrometer.core.instrument.composite.CompositeMeterRegistry
import org.junit.jupiter.api.Test
import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import java.util.concurrent.TimeUnit


class MonitorTest {


    @Test()
    fun init() {
        val registry = SimpleMeterRegistry()
    }

    @Test()
    fun testGlobalRegistry() {
        val component = MyComponent()
        /*Metrics.addRegistry(SimpleMeterRegistry())
        component.feature()
        component.feature()
        component.feature()
        component.feature()
        component.feature()*/
        val sr = SimpleMeterRegistry()
        val sr2 = SimpleMeterRegistry()
        Metrics.addRegistry(sr)
        Metrics.addRegistry(sr2)
        component.feature2("asd")
        component.feature2("asd")
        component.feature2("asd")
        component.feature2("skt")
        val c1 = Metrics.counter("asd.asd", "111", "222")
        val c2 = Metrics.counter("asd.asd")
        val ms = sr.meters
        val get = sr.get("asd.asd")
        val get2 = sr2.get("asd.asd")
        print(get.counters().size)
        print(get2.counters().size)
        print(c1.count())
        print(c2.measure())
    }

    @Test()
    fun compositeMeterRegistryTest() {
        val composite = CompositeMeterRegistry()
        val compositeCounter = composite.counter("counter")
        val simple = SimpleMeterRegistry()
        composite.add(simple)
        compositeCounter.increment()
        /*compositeCounter.increment()
        compositeCounter.increment()
        compositeCounter.increment()*/
    }

    @Test()
    fun testTimer() {
        val timer = Timer.builder("test").register(SimpleMeterRegistry()).apply {
            this.record {
                Thread.sleep(2000)
            }
        }
        timer.record {
            Thread.sleep(2000)
        }

        print(timer.mean(TimeUnit.MILLISECONDS))

        print(timer.measure())
    }
}

class MyComponent {

    companion object {
        val featureCounter = Metrics.counter("feature", "region", "test");
    }

    fun feature() {
        featureCounter.increment()
    }

    fun feature2(type: String) {
        Metrics.counter("feature.2", "type", type).increment()
    }
}